<template>
  <div class="login">
     <!-- <img class="logo" src="@/assets/images/uulab.png" alt=""> -->
     <div>
      <h2 style="text-align: center;">用户登录</h2>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        label-position="left"
        label-width="0px"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="用户名"
            style="width: 97%;"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            style="width: 97%;"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 70%;"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode">
          </div>
        </el-form-item>
        <el-row type="flex" justify="start">
          <el-col class="user-do" :span="12">
            <el-checkbox v-model="loginForm.rememberMe">
              记住我
            </el-checkbox>
          </el-col>
          <!-- <el-col class="user-do" :span="12">
            <el-link
              href="/hj-home/reset-password"
              style="margin-left: 103px;"
            >忘记密码?</el-link>
          </el-col> -->
        </el-row>
        <el-form-item style="width:100%;text-align: center;">
          <el-button
            class="logoBtn"
            :loading="loading"
            size="medium"
            type="primary"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <!-- <el-button size="medium" style="margin-left: 60px;">
            <el-link
              type="primary"
              href="/hj-home/register"
              :underline="false"
            >注 册</el-link>
          </el-button> -->
        </el-form-item>
      </el-form>
     </div>
  </div>
</template>

<script>
import {getConfigInfo} from "@/api/storehousrConfig";
import { get } from '@/api/lab.js'
import { getUsers } from '@/api/system/user.js'
import { getToken } from '@/utils/auth'
import Config from '@/settings'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/rsaEncrypt'
import { getCodeImg } from '@/api/login'
import { mapState } from 'vuex'
import { changeMenus } from '@/router/changemenu.js'
export default {
  data () {
    return {
      loading: false,
      codeUrl: '',
      loginForm: {
        username: 'admin',
        password: '123456',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },

    }
  },

  created () {
   this.getCode()
  },
  computed:{
    ...mapState('login',['isShowHome'])
  },
  methods: {
    getCookie() {
      const username = Cookies.get('username')
      let password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? '' : password
      password = password === undefined ? this.loginForm.password : password
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: ''
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid
        }
        if (user.password !== this.cookiePass) {
          user.password = encrypt(user.password)
        }
        if (valid) {
          this.loading = true
          if (user.rememberMe) {
            Cookies.set('username', user.username, {
              expires: Config.passCookieExpires
            })
            Cookies.set('password', user.password, {
              expires: Config.passCookieExpires
            })
            Cookies.set('rememberMe', user.rememberMe, {
              expires: Config.passCookieExpires
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }

          this.$store
            .dispatch('Login', user)
            .then(async () => {
              this.loading = false
              // this.$router.push('/hj-home/home')
              this.$message.success('登录成功')
              Cookies.remove('point')
              this.isLogin = getToken()
              this.$store.dispatch('app/changeSubSystem', this.$store.state.app.systemList[0])
              const res1 = await get()
              this.$store.commit('hjuser/setAllLabs',res1)
              const res = await getUsers()
              this.$store.commit('hjuser/setAllUsers',res.content)
              this.getIsShowHome()
              if(this.isShowHome==0){
                this.$router.push('/hj-home/home')
              }else {
                await changeMenus(6)
              if(this.$store.state.permission.addRouters[0].children){
                 let path = this.$store.state.permission.addRouters[0].path+'/'+this.$store.state.permission.addRouters[0].children[0].path
                 this.$router.push(path)
                }else {
                this.$message.error('您没有该系统进入权限')
               }
              }
            })
            .catch((err) => {
              this.$message.error('请核对登录信息，是否正确')
              this.loading = false
              this.getCode()
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.img
        this.loginForm.uuid = res.uuid
      })
    },
    async getIsShowHome(){
       const res = await getConfigInfo()
       this.$store.commit('login/setIsShowHome',res.single_device)
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: url('../assets/images/bg4.jpeg') no-repeat center;
  background-size: cover;
  >div {
  padding-top: 30px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 520px;
  background-color: #fff;
  border-radius: 6px;
  margin: 0 auto;
  }
  .logo {
    position: absolute;
    width: 200px;
    height: 50px;
    top: 25%;
    left: 20%;
  }
}
.login-form ::v-deep{
  border-radius: 6px;
  background: #ffffff;
  width: 520px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 45px;
    input {
      height: 45px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }

  .el-input--small .el-input__inner {
  height: 45px;
}
}

.login-code {
  width: 30%;
  display: inline-block;
  height: 45px;
  float: right;
  img {
    height: 100%;
    width: 100%;
    cursor: pointer;
    vertical-align: middle;
  }
}
.user-do {
  font-size: 16px;
  margin-top: -10px;
  margin-bottom: 15px;
}

.user-do:first-child {
  color: #888888;
}
.logoBtn{
  width: 400px;
  height: 50px;
  padding: 10px 20px;
  font-size: 20px;
}
</style>
